package com.fc.mall.controller;

import com.fc.mall.dto.CommonResult;
import com.fc.mall.model.UmsMemberLevel;
import com.fc.mall.service.UmsMemberLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员等级管理Controller
 * Created by dobuy on 2018/4/26.
 */
@Controller
@Api(tags = "UmsMemberLevelController",description = "会员等级管理")
@RequestMapping("/memberLevel")
public class UmsMemberLevelController {
    @Autowired
    private UmsMemberLevelService memberLevelService;
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation("查询所有会员等级")
    @ResponseBody
    public Object list(){
        List<UmsMemberLevel> memberLevelList = memberLevelService.list();
        return new CommonResult().success(memberLevelList);
    }

    @ApiOperation("添加会员等级定义")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(@Validated @RequestBody UmsMemberLevel umsMemberLevel,
                         BindingResult result) {
        int count = memberLevelService.create(umsMemberLevel);
        if (count > 0) {
            return new CommonResult().success(count);
        } else {
            return new CommonResult().failed();
        }
    }

    @ApiOperation("修改会员等级定义")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable Long id,
                         @Validated
                         @RequestBody UmsMemberLevel productCategoryParam,
                         BindingResult result) {
        int count = memberLevelService.update(id, productCategoryParam);
        if (count > 0) {
            return new CommonResult().success(count);
        } else {
            return new CommonResult().failed();
        }
    }

    @ApiOperation("获取会员等级定义")
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Object get(@PathVariable Long id) {
        UmsMemberLevel umsMemberLevel = memberLevelService.getItem(id);
        return new CommonResult().success(umsMemberLevel);
    }

    @ApiOperation("删除会员等级定义")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable Long id) {
        int count = memberLevelService.delete(id);
        if (count > 0) {
            return new CommonResult().success(count);
        } else {
            return new CommonResult().failed();
        }
    }
}
